package com.sendables;

import java.nio.ByteBuffer;

public class DoodlePoint extends Sendable {
	private int	x, y;

	public DoodlePoint(int x, int y)
	{
		this.x = x;
		this.y = y;
	}

	public int getX()
	{
		return x;
	}

	public void setX(int x)
	{
		this.x = x;
	}

	public int getY()
	{
		return y;
	}

	public void setY(int y)
	{
		this.y = y;
	}

	public String toString()
	{
		return getClass().getCanonicalName() + "[x=" + x + ",y=" + y + "]";
	}

	public boolean equals(Object o)
	{
		if (o != null && o.getClass().equals(this.getClass()) && o instanceof DoodlePoint)
		{
			DoodlePoint p = (DoodlePoint) o;
			if (p.x == this.x && p.y == this.y)
			{
				return true;
			}
		}
		return false;
	}

	public byte[] getByteRepresentation()
	{
		ByteBuffer bb = ByteBuffer.allocate(8);
		bb.putInt(x).array();
		bb.putInt(y).array();
		return bb.array();
	}

	public void writeByteRepresentation(byte[] b, int[] offset)
	{
		ByteBuffer.wrap(b, offset[0], b.length - offset[0]).putInt(x).putInt(y);
		offset[0] += 8;
	}

	public static void main(String[] args)
	{
		DoodlePoint p = new DoodlePoint(23424, 231);
		byte[] b = new byte[24];
		p.writeByteRepresentation(b, new int[] { 0 });
		DoodlePoint pp = DoodlePoint.parse(b, new int[] { 0 });
		System.out.println(p);
		System.out.println(pp);
	}

	public static DoodlePoint parse(byte[] b, int[] offset)
	{
		ByteBuffer bb = ByteBuffer.wrap(b, offset[0], b.length - offset[0]);
		offset[0] += 8;
		return new DoodlePoint(bb.getInt(), bb.getInt());
	}

	@Override
	public int getAlloc()
	{
		return 8; // 2*4 (2*int)
	}

}
